package com.hyr.springcloud.user.service;


import com.hyr.springcloud.dto.OrderDTO;
import com.hyr.springcloud.user.dao.PayInfoRepository;
import com.hyr.springcloud.user.dao.UserInfoRepository;
import com.hyr.springcloud.user.domain.PayInfo;
import com.hyr.springcloud.user.domain.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class UserInfoService {

    private static final Logger LOG = LoggerFactory.getLogger(UserInfoService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private PayInfoRepository payInfoRepository;



    @Transactional
    @JmsListener(destination = "order:pay" ,containerFactory="msgFactory")
    public void handleOrderPay(OrderDTO orderDTO){
        LOG.info("Get new order to pay:{}",orderDTO);

        PayInfo payInfo = payInfoRepository.findByOrderId(orderDTO.getId());
        UserInfo userInfo = userInfoRepository.getOne(orderDTO.getUserinfoId());
        System.out.println(userInfo);
        if (payInfo != null){
            LOG.warn("Order already paid, duplicated message：{}",orderDTO);
        }else {
            //返回值    -1 小于   0 等于    1 大于
            if (userInfo.getBalance().compareTo(orderDTO.getAmount()) == -1){
                LOG.warn("Not enough balance");
                orderDTO.setStatus("NOT_ENOUGH_BALANCE");
                jmsTemplate.convertAndSend("order:ticket_error",orderDTO);
                return;
            }
            payInfo = new PayInfo();
            payInfo.setAmount(orderDTO.getAmount());
            payInfo.setOrderId(orderDTO.getId());
            payInfo.setStatus("PAID");
            payInfoRepository.save(payInfo);
            //        userInfo.setBalance(userInfo.getBalance().subtract(orderDTO.getAmount()));
            userInfoRepository.charge(userInfo.getId(),orderDTO.getAmount());
        }
        orderDTO.setStatus("PAID");
        jmsTemplate.convertAndSend("order:ticket_move",orderDTO);
    }




    /**
     * 查询用户信息
     * @param id
     * @return
     */
    public Object getUserInfo(Integer id){
        Map<String,String> result = new HashMap<>();
        String sql = "select * from userinfo where id="+id;
        SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
        while (rs.next()){
            result.put("id",rs.getString("id"));
            result.put("username",rs.getString("username"));
            result.put("password",rs.getString("password"));
        }
        return result;
    }

    @Transactional
    public void updateUserInfo(String id,String username,String password){
        String update = "update userinfo set username=?,password=? where id=?";
        jdbcTemplate.update(update,username,password,id);
    }
}
